package com.jeesite.modules.own.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jeesite.modules.sys.dao.RoleDao;
import com.jeesite.modules.sys.entity.Employee;
import com.jeesite.modules.sys.entity.Role;
import com.jeesite.modules.sys.entity.User;
import com.jeesite.modules.sys.utils.EmpUtils;
import com.jeesite.modules.sys.utils.UserUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.text.DateFormat;
import java.util.Date;
import java.util.List;

import static com.jeesite.modules.Application.httpflag;

@Component
public class MessageSaveUtils {


    @Value("${qkurl.saveUrl}")
    private  String saveUrl;

    @Autowired
    private RoleDao roleDao;


    public String getTxid(String uuid,String table){

        User user = UserUtils.getUser();
        //获取职员对象
        Employee employee = EmpUtils.get(user);
        JSONObject map = new JSONObject();
        JSONObject map1 = new JSONObject();
        JSONArray arr = new JSONArray();

        Date date = new Date();
        DateFormat df2 = DateFormat.getDateTimeInstance();
        String time = df2.format(date);//当前时间
        long time1 = date.getTime();//时间戳
        String timestamp = time1+"";
        Role role = new Role();
        role.setUserCode(user.getUserCode());
        List<Role> listByUserCode = roleDao.findListByUserCode(role);
        for(Role role1:listByUserCode){
            JSONObject map2 = new JSONObject();
            map2.put("role",role1.getRoleName());
            map2.put("roleID",role1.getRoleCode());
            arr.add(map2);
        }

        map.put("uuid",uuid);
        map1.put("uuid",uuid);
        map1.put("table",table);
        map1.put("database","zyczs-dev");
        map1.put("username",user.getUserName());
        map1.put("userid",user.getUserCode());
        map1.put("roles",arr.toJSONString());
        map1.put("dep",employee.getOffice().getOfficeName());
        map1.put("depid",employee.getOffice().getOfficeCode());
        map1.put("time",time);
        map1.put("timestamp",timestamp);
        map.put("content",map1);
        String maps = map.toJSONString();

        //post方式进行http请求
        //String s = map.toJSONString();
        //System.out.println("s = " + s);


        return getTxids(maps);
    }

    public String getTxids(String maps){
        String txid="";
        //System.out.println("httpflag="+httpflag);
       // System.out.println("---------");
        if(httpflag){
            String result = HttpClient1.doPost(saveUrl,maps);
           // System.out.println("result = " + result);
            JSONObject jsonObject = JSON.parseObject(result);
            JSONObject datas = (JSONObject) jsonObject.get("datas");
            txid = (String) datas.get("txid");

        }else{
            txid="errortxid"+maps;
        }
        return txid;
    }
}
